package weaver.formmode.customjavacode.modeexpand;

import com.alibaba.fastjson.JSONObject;
import com.yunzhou.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.monitor.cache.CacheFactory;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2023-02-06
 * @Version 1.0
 * @Description TODO 船体铭牌扫码关联组件
 **/
public class CtmpToZj extends AbstractModeExpandJavaCodeNew {
    private LogTool logTool = new LogTool("/log/ctmp/CtmpToZj", false);
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        logTool.writeLog("进入模块扩展动作>船体铭牌扫码关联组件");
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    String insertSql = "insert into uf_ctmpxx_dt1(mainid,zjbm,zjmc)"
                            + "values(?,?,?)";
                    String updateSql = "update uf_zjxx set zjzt=1,sglct=? where id=?";
                    List<List<Object>> insertList = new ArrayList<>();
                    List<List<Object>> updateList = new ArrayList<>();

                    RecordSet rs1 = new RecordSet();
                    RecordSet rs2 = new RecordSet();
                    try {
                        rs1.execute("select * from uf_cmglzj where id="+billid);
                        rs1.next();
                        String ctbh = rs1.getString("ctbh");//主表船体编号
                        //扫码关联的时候可能扫重复的 进行去重
                        rs2.execute("select distinct glzj,zjmc from uf_cmglzj_dt1 where mainid="+billid);
                        while (rs2.next()){
                            String zjbmid = rs2.getString("glzj");
                            String zjmc = rs2.getString("zjmc");
                            List<Object> values1 = new ArrayList<>();
                            values1.add(ctbh);
                            values1.add(zjbmid);
                            values1.add(zjmc);
                            insertList.add(values1);

                            List<Object> values2 = new ArrayList<>();
                            values2.add(ctbh);
                            values2.add(zjbmid);
                            updateList.add(values2);
                        }
                        rst.executeBatchSql(insertSql,insertList);
                        rst.executeBatchSql(updateSql,updateList);

                        CacheFactory.getInstance().clear();
                        CacheFactory.getInstance().reset();
                    } catch (Exception e) {
                        rst.rollback();
                        logTool.writeLog("1 " + e.getMessage());
                        e.printStackTrace();
                        result.put("errmsg", "1 保存動作擴展接口異常 \n" + e.getMessage());
                        result.put("flag", "false");
                    }
                }
            }
        } catch (Exception e) {
            rst.rollback();
            e.printStackTrace();
            logTool.writeLog("2 " + e.getMessage());
            result.put("errmsg", "2 保存動作擴展接口異常 \n" + e.getMessage());
            result.put("flag", "false");
        }
        rst.commit();
        return result;
    }
}
